import uuid

from fastapi import APIRouter, Depends, Query

from admins.admin import create_new_admin
from admins.user import create_new_user, fetch_user_info_record, update_user_info_record, delete_user_record
from authentication.jwt import check_admin_privilege, get_current_user
from domain.authentication import Admin, User

admin_router = APIRouter(
    prefix="/administration",
    tags=['admin']
)


@admin_router.post("/admin/create")
async def create_new_admin_with_current_user(
        new_admin_info: Admin,
        authenticated_admin: Admin = Depends(check_admin_privilege)
):
    may_be_new_admin = await create_new_admin(
        new_admin=new_admin_info,
        auth=authenticated_admin
    )
    return may_be_new_admin


@admin_router.post("/user/create")
async def sign_up_new_user(
        new_user_info: User,
        authenticated_admin: Admin = Depends(check_admin_privilege),
):
    user_create_response = await create_new_user(
        new_user=new_user_info,
        authenticated_admin=authenticated_admin
    )
    return user_create_response


@admin_router.get("/user/fetch")
async def get_user_info(
        username: str = Query(
            ...,
            description="Target User username.",
        ),
        email: str = Query(
            ...,
            description="Target User email.",
        ),
        current_user: User = Depends(get_current_user)
):
    user_uuid = uuid.uuid5(
        uuid.NAMESPACE_DNS,
        f"{username}:{email}"
    )
    user_info_record = await fetch_user_info_record(
        user_uuid=user_uuid,
        user=current_user
    )
    return user_info_record


@admin_router.patch("/user/update")
async def update_user_info(
        user: User,
        user_record_id: str = Query(
            ...,
            alias="user-record-id",
            title="Target User record id.",
            description="Record id of the User intended to get information about.",
        ),
        current_user: User = Depends(get_current_user)
):
    user_info_record = await update_user_info_record(
        new_user_info=user,
        record_id=user_record_id,
        user=current_user
    )
    return user_info_record


@admin_router.delete("/user/delete")
async def delete_user(
        user_record_id: str = Query(
            ...,
            alias="user-record-id",
            title="Target User record id.",
            description="Record id of the User intended to be deleted.",
        ),
        authenticated_admin: Admin = Depends(check_admin_privilege),
):
    ret = await delete_user_record(
        record_id=user_record_id,
        authenticated_admin=authenticated_admin
    )
    return ret
